<#include '/admin/header.html' >

<style>
.goods-info{
	background-color: white;
	margin-top: 10px;
}
.layui-elem-field legend{
	margin-left: 0px;
	padding:  0px 30px;
	margin: 0px;
}
.goods-content{
	margin-left: 20px;
	margin-right: 20px;
}
</style>
<div class="admin-main">
	<form class="layui-form layui-form-pane" id="theForm" >
	
		<#if is_edit==true>
			<input value="${ctx}/shop/admin/goods/save-edit.do" type="hidden" id="goods_inputAction" />
		 	<input type="hidden" name="isedit" value="yes" />
			<input type="hidden" name="goods_id" value="${goodsView.goods_id  }" />
		<#else>
			<input value="${ctx}/shop/admin/goods/save-add.do" type="hidden" id="goods_inputAction" />
			<input type="hidden" id="isedit" name="isedit" value="no" />
			<input type="hidden" id="goods_id"  name="goods_id" value="${(goodsView.goods_id)!''}" />
		</#if>
		<input id="preview_goods_id" type="hidden" value="${(goodsView.goods_id)!'0'}" >
			<div style="display: block;" class="goods_input">
				<div class="contentTab">
					<ul class="tab">
		
		<#list pluginTabs as tab>
			<div class="goods-info">
				<fieldset class="layui-elem-field layui-field-title">
					<legend>${tab.tabTitle}</legend>
				</fieldset>
				
				<div class="goods-content">
					${tab.tabHtml}
				</div>
			</div>
		</#list>

		<div class="goods-info">
			<div class="goods-content">
				<div class="layui-form-item">
				    <div class="layui-input-block">
				      <button class="layui-btn" id="save" lay-submit="" lay-filter="goodsinput">立即提交</button>
				      <#if is_edit==true>
			          <#else>
				       <a class="layui-btn" lay-submit  id="draft" lay-filter="draft">保存草稿</a>
				      </#if>
				     <button type="reset" class="layui-btn layui-btn-primary">重置</button>
				    </div>
			  </div>
			</div>
		</div>
	</form>
</div>

<script>


layui.use(['element', 'form'], function(){
	var element = layui.element();
	var form = layui.form();
    var t;
		//如果是 (修改商品 && 草稿商品)||添加商品 ，启动自动保存
		<#if (is_edit==true && goodsView.market_enable==2) || is_edit!=true >
			//防止初始化后即刻执行，延时30秒执行
			setTimeout(autoSaveGoods, 1000*30);
		</#if>
		
		$(".goods_input .contentTab>ul>li").click(function() {
			var tabid = $(this).attr("tabid");
			$(".goods_input .contentTab>ul>li").removeClass("contentTabS");
			$(this).addClass("contentTabS");
			$(".tab-page .tab-panel").hide();
			$(".tab-panel[tabid=" + tabid + "]").show();
		});
		
	//立即提交
	form.on('submit(goodsinput)', function(data) {
		
		//如果商品开启了规格，就必须添加规格，否则无法保存	add by DMRain 2016-2-191
		if($("#haveSpec").val() == 1){
			if($("#spec-input .detail .spec_table tbody tr").length == 0){
				$.Loading.error("您已开启规格，但是还未添加规格，请添加后再保存！");
				return false;
			}
		} 
		//图片上传判定
		if(typeof($("input[name='picnames']").val()) == "undefined"){
			$.Loading.error("图片必须上传！");
			return false;
		}
		// 判定设置库存是否大于实际库存
		if(parseInt($(".stock").val()) > parseInt($(".stock").attr("rel"))){
			$.Loading.error("对不起，库存不足！");
			return false;
		}
		
		var options = {
			url : $("#goods_inputAction").val(),
			type : "POST",
			dataType : "json",
			beforeSend:function(){ //触发ajax请求开始时执行  			
	   			/* $('#save').attr('onclick','javascript:void();');//改变提交按钮上的文字并将按钮设置为不可点击
	   			}, */
	   		 $('#save').attr("disabled", true);	
			}, 
			success : function(result) {
				if (result.result == 1) {
						$.Loading.success(result.message);
						closeCurrent();
				}else{
					$.Loading.error(result.message);
					$("#goodsinput").linkbutton("enable");
				}
			},
			error : function(e) {
				$.Loading.success("出错了,请重试");
				$("#goodsinput").linkbutton("enable");
			}
		};
		
		$('#theForm').ajaxSubmit(options);
		
		return false;
	});
	
	//保存草稿
	$("#draft").click(function(){
		autoSaveGoods();
	})
	
	function autoSaveGoods(){
		//间隔秒数
		var time = 30;
		var url;
		if($("#preview_goods_id").val()==0){
			url="${ctx}/shop/admin/goods/add-draft-goods.do";
		}else{
			url="${ctx}/shop/admin/goods/edit-draft-goods.do?preview_goods_id="+$("#preview_goods_id").val();
		}
		var options = {
		url : url,
		type : "POST",
		dataType : "json",
		success : function(data) {
			if (data.result == 1) {
				$("#preview_goods_id").val(data.data.goods_id);
				$("#goods_inputAction").val("${ctx}/shop/admin/goods/save-edit.do");
				$("#isedit").val("yes");
				$("#goods_id").val(data.data.goods_id);
				if(data.data.market_enable==2){
					$.Loading.success("已自动保存到商品草稿箱！");
					t = setTimeout(autoSaveGoods, 1000*time);
				}else{
					$.Loading.success("上架商品无法保存为草稿");
				}
			}else{
				$.Loading.success("自动保存出错：" + data.message);
				t = setTimeout(autoSaveGoods, 1000*time);
			}
		},
		error : function(e) {
			$.Loading.success("出错了,请重试");
			t = setTimeout(autoSaveGoods, 1000*time);
		}
	};
		$('#theForm').ajaxSubmit(options);
		return false;
        }
});

</script>


<#include '/admin/footer.html' >
